package com.sxhalo.fsms.service.impl;

import com.alibaba.fastjson.JSON;
import com.sxhalo.fsms.domain.FactDevice;
import com.sxhalo.fsms.domain.RedisData;
import com.sxhalo.fsms.mapper.FactDeviceMapper;
import com.sxhalo.fsms.service.IFactDeviceService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.stereotype.Service;
//import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class FactDeviceServiceImpl implements IFactDeviceService {


	@Autowired
	private  FactDeviceMapper mapper;
	@Autowired
	private RedisTemplate<String, ?> redisTemplate;

	@Override
	public Integer insertSelective(FactDevice record) {
		return mapper.insertSelective(record);
	}

	@Override
	public Integer updateByPrimaryKeySelective(FactDevice record) {
		return mapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public Integer deleteByPrimaryKey(Object key) {
		return mapper.deleteByPrimaryKey(Integer.parseInt(key.toString()));
	}

	@Override
	public  FactDevice selectByPrimaryKey(Object key) {
		return mapper.selectByPrimaryKey(Integer.parseInt(key.toString()));
	}

	@Override
	public List< FactDevice> findWhereList(String where, String order) {
		return mapper.findWhereList(where, order);
	}

	@Override
	public Integer getCount(String where) {
		return mapper.getCount(where);
	}

	@Override
	public List<FactDevice> findPagerList(Integer start, Integer length, String where, String order) {
		return mapper.findPagerList(start, length, where, order);
	}



	@Override
	public int batchInsert(FactDevice record, Integer num,String devicename) {
//		int part=(int) Math.ceil(num/100);
//	   for(int i=0;i<part;i++){
//		if(i==part-1){
//			mapper.batchInsert(record,num,devicename);
//		}else{
//			mapper.batchInsert(record,100,devicename);
//		}
//		   num-=100;
//	   }
//
//		return  0;

		return mapper.batchInsert(record,num,devicename);
	}

	@Override
	public int getChildCount(String where, Integer parentId) {
		return mapper.getChildCount(where,parentId);
	}

	@Override
	public List<FactDevice> findChildPagerList(Integer start, Integer length, String where, String order, Integer parentId) {
		return mapper.findChildPagerList(start, length,  where,  order,parentId);
	}

	@Override
	public List<FactDevice> findRealDataPagerList(Integer start, Integer length, String where, String order, Integer parentId) {
		return mapper.findRealDataPagerList(start, length,  where,  order,parentId);
	}


	@Override
	public int batchInsert(List<FactDevice> list) {
		return mapper.batchInsert1(list);
	}

	@Override
	public int batchUpdate(FactDevice record, String where) {
		return mapper.batchUpdate(record,where);
	}

	@Override
	public RedisData getLiveData(Integer deviceId) {
		try{
			Map<Object, Object> map  = redisTemplate.opsForHash().entries(deviceId.toString());
			RedisData data= JSON.parseObject(JSON.toJSONString(map),RedisData.class);
			return data;
		}catch(Exception e){
			 System.out.println(e);
		}
		return null;
	}

	@Override
	public Map<String, Object> getStatistics(String where) {
		return mapper.getStatistics(where);
	}

	@Override
	public List<FactDevice> getTypeList(String where) {
		return mapper.getTypeList(where);
	}
	@Override
	public int deleteByWhere(String where) {
		return mapper.deleteByWhere(where);
	}

	@Override
	public int getRealDataCount(String where, Integer parentId) {
		return mapper.getRealDataCount(where,parentId);
	}

	@Override
	public  List<HashMap> findAttrHis(String  attr, String   where, String  tableName){
		return  mapper.findAttrHis(attr,where,tableName);
	}
}
